// pages/todoDetail/todoDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        detail:'',
        time:'',
        classify:[],
        judge:[], //判断当前分类是否被选中
        curYear: new Date().getFullYear(), 
        curMonth: new Date().getMonth() + 1,
        day: new Date().getDate(),
        showCal:false,
        showList:false,
        input:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let d=JSON.parse(options.detail)
        let stampToStr=new Date((d.time)).toLocaleString().slice(0,10).replace(/\//g, '-');
        let year=stampToStr.slice(0,4)
        let month=stampToStr.slice(5,7)
        let day
        if(month[1]=='-'){
            month=month[0]
            day=stampToStr.slice(7,9)
        }
        else{
            day=stampToStr.slice(8,10)
        }
        if(day[1]==' ')
            day=day[0]
        /*
            console.log(stampToStr)
            console.log(year+"-year")
            console.log(month+"-month")
            console.log(day+"-day")
        */
        if(d.hasTime==true){
            this.setData({
                detail:d,
                curYear:year,
                curMonth:month,
                day:day,
                showCal:d.hasTime
            })
        }
        else{
            this.setData({
                detail:d,
                showCal:d.hasTime
            })
        }
        
        //+0处理
        month=month<10?'0'+month:month
        day=day<10?'0'+day:day

        var that=this
        var timeStr=year+'-'+month+"-"+day
        console.log(timeStr)
        let idx=0,len=0
        wx.cloud.callFunction({
            name: 'getClassification',
            success:function (res) {
                for(idx = 0,len=res.result.length; idx < len; idx++) {
                    if(res.result[idx]==that.data.detail.classify) {break}
                }
                let index='judge['+idx+']'
              that.setData({
                classify:res.result,
                time:timeStr,
                judge:new Array(res.result.length).fill(0),
                [index]:1
              })
            }
        })
    },

    done:function(){
        var that=this
        var subTitle=this.data.detail.title
        var subClassify=this.data.detail.classify
        if(subClassify==''&&subTitle==''){
            wx.showModal({
                title: '提示',
                content: '请输入标题并选择列表后再试',
            })
        }
        else if(subClassify==''&&subTitle!=''){
            wx.showModal({
                title: '提示',
                content: '请选择列表后再试',
            })
        }
        else if(subClassify!=''&&subTitle==''){
            wx.showModal({
                title: '提示',
                content: '请输入标题后再试',
            })
        }
        else{
            let stamp=Number(new Date(this.data.time))
            let checkStr=this.data.detail.classify+' '+this.data.detail.title+' '+this.data.detail.description
            wx.cloud.callFunction({
                name: 'msgCheck',
                data:{
                  content:checkStr
                },
                success:function (res1) {
                    if(res1.result.result.suggest==="pass"){
                        wx.cloud.callFunction({
                            name: 'updateTodo',
                            data:{
                                _id:that.data.detail._id,
                                title:subTitle, 
                                desc:that.data.detail.description,
                                time:stamp,
                                hasTime:that.data.showCal,
                                classify:subClassify,
                                done:that.data.detail.done
                            },
                            success:function () {
                              wx.navigateBack({
                                  delta:1
                              })
                            }
                        })
                    }
                    else{
                        wx.showModal({
                            title: '提示',
                            content: '当前输入包含违规内容，请重新输入后再试',
                            success: function() {
                                that.onShow()
                            }
                        })
                    }
                }
            })
        }
    },

    delete:function(){
        var that=this
        wx.showModal({
            title: '提示',
            content: '确定删除当前待办?',
            success: function(res) {
              if (res.confirm) {
                //console.log("delete")
                wx.cloud.callFunction({
                    name: 'deleteTodo',
                    data:{
                        _id:that.data.detail._id
                    },
                    success:function (res1) {
                        wx.navigateBack({
                          delta: 1,
                        })
                    }
                })
                }
            }
        })    
    },

    selectClassify:function(e){
      var c=e.currentTarget.dataset.class
      var idx=e.currentTarget.dataset.idx
      var m='judge['+idx+']'
      var d='detail.classify'
      this.setData({
          [d]:c,
          judge:new Array(this.data.judge.length).fill(0),
          [m]:1,
          input:false
      })
    },

    switchDone:function(){
        let str='detail.done'
        this.setData({
            [str]:!this.data.detail.done
        })
    },

    unfoldList:function(){
        this.setData({
            showList:!this.data.showList
        })
    },

    selectDate:function(e){
        let str=e.detail.date.split('-')
        this.setData({
            time:e.detail.date,
            curYear:str[0],
            curMonth:str[1],
            day:str[2]
        })
    },

    showCal:function(){
        this.setData({
            showCal:!this.data.showCal,
        })
    },

    recTitle:function (e) {
        var str='detail.title'
        this.setData({
           [str]:e.detail.value
        })
    },

    inputClassify:function(e){
        var d='detail.classify'
        this.setData({
            input:true,
            [d]:e.detail.value,
            judge:new Array(this.data.judge.length).fill(0)
        })
    },

    recDesc:function (e) {
        var str='detail.description'
        this.setData({
           [str]:e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return{
            title:'',
            path:'/pages/index/index',
            imageUrl:'/images/icon.png',
            success:(res)=>{
              
            }
        }
    }
})